package com.adminpcBo;

import java.util.regex.Matcher;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;

public class validation {
	public void validateEmail(FacesContext context, UIComponent validate, Object value) {
		  
		 // String message = "";
		  String log = (String) value;
		  if (log.isEmpty()) {
		    ((UIInput)validate).setValid(false);
		    FacesMessage msg = new FacesMessage("Invalid Email");
		    msg.setDetail("Name Field is Blank..");
           context.addMessage(validate.getClientId((context)), msg);
           
		  }
     }
	public void ipadresse (FacesContext context,UIComponent validate, Object value) {
		  
		  
		  String ip = (String) value;
		  if (testeip(ip) == false) {
		  ((UIInput)validate).setValid(false);
		  FacesMessage msg = new FacesMessage("Adresse ip fausse");
		  msg.setDetail("Adresse ip fausse");
		  //context.addMessage("myForm:ip",msg);
          context.addMessage(validate.getClientId((context)), msg);
          
		  }
    }
    
	public void Dns(FacesContext context, UIComponent validate, Object value) {
		  
		 
		  String dns = (String) value;
		  if (testeip(dns) == false) {
		  ((UIInput)validate).setValid(false);
		  FacesMessage msg = new FacesMessage("Adresse ip fausse");
		  msg.setDetail("Dns");
        context.addMessage(validate.getClientId((context)), msg);
        
		  }
  }
	
	public boolean testeip(String ip){
		String[] tabIp = ip.split("\\.");
		boolean isIp = Boolean.TRUE;
		try {
			if (tabIp.length!=4) return false;
			int digitIp =0; 
			for (int i=0;i<tabIp.length;i++)
			{
				digitIp = Integer.parseInt(tabIp[i]);
				if (digitIp<0 || digitIp>255)
					isIp = Boolean.FALSE;
			}
		} catch (Exception e) {
			isIp = Boolean.FALSE;
		}
		return isIp;
	}
}